﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Rison.BusinessFacade.Entity.XT;
using Rison.BusinessFacade.Facade.XT;
using Rison.DataAccess.Common;

namespace Rison.BusinessFacade.Common
{
    public sealed class CurrentLoginedUser
    {
        private CurrentLoginedUser()
        {
            //ReadUser("admin");
        }
        public static void SetCurrentUserId(string userid, string pwd,string type)
        {
            ReadUser(userid, pwd,type);
         }
        public static void SetCurrentUser(string userid, string username, string pwd)
        {
            HttpContext.Current.Session.Add("CurrentLoginedUserId", userid);
            HttpContext.Current.Session["CurrentLoginedUserName"] = username;
            HttpContext.Current.Session["CurrentLoginedPassword"] = pwd;
        }
        public static void ClearUserSession()
        {
            if (HttpContext.Current.Session["CurrentLoginedUserId"] != null)
            {
                HttpContext.Current.Session.Remove("CurrentLoginedUserId");
            }

            if (HttpContext.Current.Session["CurrentLoginedUID"] != null)
            {
                HttpContext.Current.Session.Remove("CurrentLoginedUID");
            }

            if (HttpContext.Current.Session["CurrentLoginedUserName"] != null)
            {
                HttpContext.Current.Session.Remove("CurrentLoginedUserName");
            }

            if (HttpContext.Current.Session["CurrentLoginedPassword"] != null)
            {
                HttpContext.Current.Session.Remove("CurrentLoginedPassword");
            }
        }

        private static void ReadUser(string userid, string pwd,string type)
        {
            ClearUserSession();

            if (!string.IsNullOrEmpty(userid))
            {
                SqlField f = new SqlField("UserId", userid);
                List<SqlField> fields = new List<SqlField>();
                fields.Add(f);

                SysUserFacade facade = new SysUserFacade();
                SysUser user = facade.Retrieve(fields) as SysUser;

                if (user != null)
                {
                    if (user.Type != "1" && user.Password.Equals(pwd))
                    {
                        HttpContext.Current.Session.Add("CurrentLoginedUserId", userid);
                        HttpContext.Current.Session["CurrentLoginedUID"] = user.UID;
                        HttpContext.Current.Session["CurrentLoginedUserName"] = user.Name;
                        HttpContext.Current.Session["CurrentLoginedPassword"] = user.Password;
                    }
                    else if (user.Type == "1")
                    {
                        HttpContext.Current.Session.Add("CurrentLoginedUserId", userid);
                        HttpContext.Current.Session["CurrentLoginedUID"] = user.UID;
                        HttpContext.Current.Session["CurrentLoginedUserName"] = user.Name;
                        HttpContext.Current.Session["CurrentLoginedPassword"] = "";
                    }
                }


                facade = null; user = null; fields = null; f = null;
            }
        }

        public static bool Logined
        {
            get { return !string.IsNullOrEmpty(UserId); }
        }

        public static int UID
        {
            get
            {
                if (HttpContext.Current.Session["CurrentLoginedUID"] != null)
                    return Convert.ToInt32(HttpContext.Current.Session["CurrentLoginedUID"]);

                return 0;
            }
        }

        public static string UserId
        {
            get
            {
                if (HttpContext.Current.Session["CurrentLoginedUserId"] != null)
                    return HttpContext.Current.Session["CurrentLoginedUserId"].ToString();
                
                return string.Empty;
            }
        }

        public static string UserName
        {
            get
            {
                if (HttpContext.Current.Session["CurrentLoginedUserName"] != null)
                    return HttpContext.Current.Session["CurrentLoginedUserName"].ToString();

                return string.Empty;
            }
        }

        public static string Password
        {
            get
            {
                if (HttpContext.Current.Session["CurrentLoginedPassword"] != null)
                    return HttpContext.Current.Session["CurrentLoginedPassword"].ToString();
              
                return "undefined";
            }
            set
            {
                HttpContext.Current.Session["CurrentLoginedPassword"] = value;
            }
        }


    }
}
